package win.smartown.android.base

import android.os.Bundle
import android.support.annotation.IdRes
import android.support.annotation.LayoutRes
import android.support.v7.widget.Toolbar
import android.util.Log
import android.view.View
import android.view.ViewStub

import win.smartown.android.R

/**
 * Created by smartown on 2017/3/18 17:10.
 * Desc:
 */
open class BaseActivity : SuperActivity() {

    private var baseContentView: View? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_base)
        setupToolbar()
    }

    protected fun setupToolbar() {
        super.findView<View>(R.id.base_shadow).visibility = View.VISIBLE
        val toolbar = super.findView<Toolbar>(R.id.base_toolbar)
        val title = intent.getStringExtra("title")
        try {
            toolbar.title = title
        } catch (e: Exception) {
            Log.e("BaseActivity", "please use JumpUtil.java to start an activity!")
        }

        setSupportActionBar(toolbar)
        toolbar.visibility = View.VISIBLE
        toolbar.setNavigationOnClickListener { onGoBack() }
    }

    protected fun setBaseContentView(@LayoutRes id: Int) {
        val viewStub = super.findView<ViewStub>(R.id.base_content)
        viewStub.layoutResource = id
        baseContentView = viewStub.inflate()
    }

    override fun <T : View> findView(@IdRes id: Int): T {
        return baseContentView!!.findViewById(id) as T
    }

    protected fun onGoBack() {
        finish()
    }

}
